package com.tanhua.server.controller;

import com.tanhua.model.domain.UserInfo;
import com.tanhua.server.interceptor.ThreadLocalUtils;
import com.tanhua.server.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/user")
public class UserController {


    @Autowired
    private UserInfoService userInfoService;

    /**
     *
     * 用户信息完善：把数据保存到数据库
     * 1.请求路径：/user/loginReginfo
     * 参数：UserInfo,token
     */
    @PostMapping("/loginReginfo")
    public ResponseEntity loginReginfo(@RequestBody UserInfo userInfo,
                                       @RequestHeader("Authorization") String token){

        //1.校验token已经在拦截器中完成
        //2.解析token，为UserInfo设置id，跟User对应
        userInfo.setId(Long.valueOf(ThreadLocalUtils.getUser().getId()));
        //3.调用UserInfoServer保存数据
        userInfoService.save(userInfo);
        return ResponseEntity.ok(null);
    }

    /**
     *
     * 用户上传头像；
     * 1.请求路径：/user/loginReginfo/head
     * 2.请求方式：post
     * 3.请求参数：multipart(用户上传的头像),token
     * 4.响应结果：无
     */
    @RequestMapping("/loginReginfo/head")
    public ResponseEntity uploadUserHeadImage(MultipartFile headPhoto,
                                      @RequestHeader("Authorization") String token ) throws IOException {


        //1.校验token已经在拦截器中完成
        //2.从token中获取用户id

        //3.调用UserInfoService
        userInfoService.uploadUserHeadImage(headPhoto,ThreadLocalUtils.getUserId());
        return  ResponseEntity.ok(null);
    }
}
